<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head th:replace="component/head :: head"></head>

<body onload="getRequest();fillHref()">
	<header th:replace="component/header :: header"></header>

	<!-- Begin page content -->
	<div class="container">
		<div class="row">
			<div th:replace="component/sidebar"></div>

			<div class="col-md-9">
				<div th:replace="component/page-title::pageTitle('图书详情')"></div>

				<div class="row">
					<div class="col-md-4 offset-md-4 mb-3"></div>
				</div>

				<div class="row">
					<div class="col-md-8 offset-md-2">
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">书名</span>
							</div>
							<input type="text" class="form-control" id="basic-url-name"
								aria-describedby="basic-addon3" readonly>
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">书编</span>
							</div>
							<input type="text" class="form-control" id="basic-url-number"
								aria-describedby="basic-addon3" readonly>
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">出版社</span>
							</div>
							<input type="text" class="form-control" id="basic-url-press"
								aria-describedby="basic-addon3" readonly>
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">作者</span>
							</div>
							<input type="text" class="form-control" id="basic-url-author"
								aria-describedby="basic-addon3" readonly>
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">库存</span>
							</div>
							<input type="text" class="form-control" id="basic-url-stock"
								aria-describedby="basic-addon3" readonly>
						</div>
					</div>
				</div>


				<div class="row">
					<div class="col-md-8 offset-md-2">
						<a href="#" class="btn btn-primary" role="button"
							id="book-modify-url">修改信息</a>
						<button onclick="deleteBook()" type="button"
							class="btn btn-danger">删除该书</button>
						<a href="#" class="btn btn-secondary" role="button"
							id="book-list-url">返回列表页</a>
					</div>
				</div>
			</div>
		</div>
	</div>

	<footer th:replace="component/footer :: footer"></footer>

	<script>
		function getUrl() {
			var url = location.search; //获取url中"?"符后的字串
			var theRequest = new Object();
			if (url.indexOf("?") != -1) {
				var str = url.substr(1);
				strs = str.split("&");
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = unescape(strs[i]
							.split("=")[1]);
				}
			}
			return theRequest;
		}

		function getRequest() {
			var theRequest = new Object();
			theRequest = getUrl();
			//获取id
			var bookId = theRequest["id"];
			$
					.ajax({
						url : "/restmetadata/get-bookvo-by-bookid",
						type : "POST",
						data : JSON.stringify(bookId),
						contentType : "application/json; charset=utf-8",
						dataType : 'json',
						async : true,
						success : function(result) {
							if (result.success == false) {
								if (result.code == 9002) {
									alert("参数错误");
								} else if (result.code == 9999) {
									alert("系统错误");
								}
							} else {
								var bookVO = result.module;
								document.getElementById('basic-url-name').value = bookVO.name;
								document.getElementById('basic-url-number').value = bookVO.number;
								document.getElementById('basic-url-press').value = bookVO.press;
								document.getElementById('basic-url-author').value = bookVO.author;
								document.getElementById('basic-url-stock').value = bookVO.stock;
							}
						},
						error : function(e) {
							alert("异常");
						}
					});
		}

		function deleteBook() {
			var msg = "确定要删除吗？";
			if (confirm(msg) == true) {
				var theRequest = new Object();
				theRequest = getUrl();
				var id = theRequest["id"];
				$
						.ajax({
							url : "/restmetadata/delete-book-by-id",
							type : "POST",
							data : id,
							contentType : "application/json; charset=utf-8",
							dataType : 'json',
							async : true,
							success : function(result) {
								if (result.success == false) {
									if (result.code == 9002) {
										alert("参数错误");
									} else if (result.code == 9999) {
										alert("系统错误");
									}
								} else {
									alert("删除成功！");
									window.location.href = "/metadata/book-list";
								}
							},
							error : function(e) {
								alert("异常");
							}
						});
			} else {
				return;
			}
		}

		var url;
		function getHref(id) {
			$.ajax({
				url : "/restmetadata/get-book-modify-url",
				type : "POST",
				data : id,
				contentType : "application/json; charset=utf-8",
				dataType : 'json',
				async : false,
				success : function(result) {
					if (result.success == false) {
						if (result.code == 9002) {
							alert("参数错误");
						} else if (result.code == 9999) {
							alert("系统错误");
						}
					} else {
						url = result.module;
					}
				},
				error : function(e) {
					alert("异常");
				}
			});
			return url;
		}

		function redirectUrl() {
			var theRequest = new Object();
			theRequest = getUrl();
			var id = theRequest["id"];
			var href = getHref(id);
			return href;
		}

		function fillHref() {
			document.getElementById("book-modify-url").href = redirectUrl();
			document.getElementById("book-list-url").href = "/metadata/book-list";
		}
	</script>

	<div th:replace="component/footer-lib"></div>
</body>

</html>